package swa2.icecream.command;

import java.util.Stack;

public class Invoker
{
	private static final int SLOTS = 4;
	
	private Command[] onCmds;
	private Command[] offCmds;

	private Stack<Command> executedCmds;
	
	public Invoker()
	{
		onCmds = new Command[SLOTS];
		offCmds = new Command[SLOTS];
		executedCmds = new Stack<Command>();
	}
	
	public boolean setCommand(int slot, Command onCmd, Command offCmd)
	{
		if(slot >= SLOTS || slot < 0)
			return false;
		
		onCmds[slot] = onCmd;
		offCmds[slot] = offCmd;
		
		return true;
	}
	
	public boolean executeOnCmd(int slot)
	{
		if(slot >= SLOTS || slot < 0)
			return false;
		
		if(onCmds[slot] == null)
			return false;
		
		onCmds[slot].execute();

		executedCmds.push(onCmds[slot]);
		
		return true;
	}
	
	public boolean executeOffCmd(int slot)
	{
		if(slot >= SLOTS || slot < 0)
			return false;
		
		if(offCmds[slot] == null)
			return false;
		
		offCmds[slot].execute();

		executedCmds.push(offCmds[slot]);

		return true;
	}
	
	public void executeUndoCmd()
	{
		if(executedCmds.isEmpty())
			return;

		((Command) executedCmds.pop()).undo();
	}
}
